Skip to main content

Aggregating data

ArangoDB supports aggregation of data via collect

  1. Let’s determine the number of airports by state

    for airport in airports
    collect state = airport.state with count into total
    return {
    state: state,
    'airports total': total
    }
  2. Here is a snippet of the expected results:

    [
    {
    "state": "AK",
    "airports total": 263
    },
    {
    "state": "AL",
    "airports total": 73
    },
    {
    "state": "AR",
    "airports total": 74
    },
    {
    "state": "AS",
    "airports total": 3
    },
    {
    "state": "AZ",
    "airports total": 59
    },
    {
    "state": "CA",
    "airports total": 205
    },
    {
    "state": "CO",
    "airports total": 49
    },
  3. You can also sort the results

    for airport in airports
    collect state = airport.state WITH COUNT INTO total
    sort total desc
    return {
    state: state,
    'airports total': total
    }
  4. Here is a snippet of the expected results:

    [
    {
    "state": "AK",
    "airports total": 263
    },
    {
    "state": "TX",
    "airports total": 209
    },
    {
    "state": "CA",
    "airports total": 205
    },
    {
    "state": "OK",
    "airports total": 102
    },
    {
    "state": "OH",
    "airports total": 100
    },
    {
    "state": "FL",
    "airports total": 100
    },
    {
    "state": "NY",
    "airports total": 97
    },
  5. Let's also determine the longest and shortest flights in the dataset

    for flight in flights
    collect aggregate
    minDistance = min(flight.Distance),
    maxDistance = max(flight.Distance)

    return {
    "Shortest flight": minDistance,
    "Longest flight": maxDistance
    }
  6. Here is the expected result:

    [
    {
    "Shortest flight": 24,
    "Longest flight": 4962
    }
    ]
 
Help us improve

Anything unclear or buggy in this tutorial? Provide Feedback